#ifndef _QI_H
#define _QI_H


#include "base_types.h"
#include "cw32l010.h"
#include "cw32l010_systick.h"
#include "cw32l010_atim.h"
#include "cw32l010_btim.h"
#include "cw32l010_flash.h"
#include "cw32l010_gpio.h"
#include "cw32l010_uart.h"
#include "cw32l010_adc.h"
#include "cw32l010_sysctrl.h"
#include "cw32l010_spi.h"
//#include "cw32l010_pwr.h"
//#include "cw32f003_rcc.h"

//#include "interrupts_cw32f003.h"
//#include "cw32f003_vc.h"


typedef 	unsigned char	u8;
typedef 	unsigned short	u16;
typedef 	unsigned long	u32;


#define HALFDUTY	95
#define FUORDUTY	32

#define HIGHDUTY	95
#define LOWDUTY		88


#define IO_TOGGLE	PA05_TOG()
#define FRE128K		188
#define FRE125K		192
#define PINTIME		480

#define DCURR	3
#define DVIN	11
#define TEMP	8

#define LEDRH		PA01_SETHIGH()
#define LEDRL		PA01_SETLOW()
#define LEDBH		PA00_SETHIGH()
#define LEDBL		PA00_SETLOW()

#define FANON		PA06_SETHIGH()
#define FANOFF		PA06_SETLOW()

#define LEDTOGGLE	PA00_TOG();PA01_TOG()

#define	DP0_6	PA02_SETHIGH()
#define	DP3_3	PB00_SETHIGH()

//#define IPHON_TANCHUANG-
#define DFOD_


#define FODFULLVALUE		78		//126
#define FODHALFVALUE		39

#define DEADTIME 6
#define STATIC_FODVALUE		0x300

typedef enum
{
    Ping_Phase=0,
    Identify_Config_Phase,
    Negotiation_Phase,
	MPP_Negotiation_Phase,
    Power_Transfer_Phase,
    Charge_Finish_Phase,
}QI_POWER_PHASE_TYPEDEF;


typedef struct
{
    u8  Message[20];
    volatile u8  CheckSum;
    volatile u8  Flag;
}QI_DATA_PACKET_TYPEDEF;

typedef struct
{
	u16 max_power;
	u16 received_value;
	u8 power_packet;
	//u16 MAXfodTime;
	//u16 current_adc;
	u16 power_receive;
	u8 errTimes;
	u16 Char_FodValue;
	u8 Char_Fodtime;
	u8 Char_fod_state;
}QIDDFOD;
extern QIDDFOD DfodStruct;


extern volatile u8 WPCQi_Phase;
extern volatile QI_DATA_PACKET_TYPEDEF               Qi_Packet;

extern volatile u16 TIM1_frequency;
extern volatile u16 TIM1_duty;
extern volatile u8 PWM_State;
extern volatile u16 Voltage_State;
extern volatile u16 Timer_Counter;
extern u8 open_four_state;
extern volatile u8 DeviceName;
extern volatile u16 T0_Timer;
extern u16 Fos_MOD;
extern u16 Fos_OP;
extern u16 fod_delay_time;
extern u8 fod_times;
extern u16 T0_shadow;
extern u16 offset_adc;
extern volatile u8 re_header;
extern volatile u8 dis_TIM1;
extern volatile u8 dis_Mag;
extern volatile u8 Have_Received_18;
extern u8 nego_Device;
extern u8 open_four_imm;
extern u8 fod_state;
extern u8 over_tem_state;
extern u8 suppoprt_9v;
extern u8 suppose_to_open_Four;
extern u8 OverVola;
extern u8 ex_onetime;
extern volatile u8 en_change_Fre;
extern u8 nack;
extern u8 ping_state;


extern u8 mpp_stage;
extern u8 dis_close;

extern volatile u8 send_bit;
extern volatile u8 bit_finish;
extern volatile u8 send_stage;
extern volatile u8 wait_finish;

extern u16 periodTime;


extern volatile u8 AppleStage;
extern volatile u8 AppleStageTimer;


extern u8 h_tem_state;
extern volatile u8 A1Duty;
extern volatile u8 B1duty;

extern volatile u8 appleTanchuagn;

extern u8 testvalue;
extern volatile u8 time_out;
//extern u8 SamLockPower;

//LED

extern u8 err;
extern u8 timer;
extern u8 x;
extern u8 y;
extern u8 z;
extern u8 MaxValue;
extern u8 Step;

extern u8 QCState;
extern u16 QCtimer;


void SendDat(u8 G,u8 R,u8 B);
void ColorCircle(void);

void ColorCircle_st(void);
void gpio_init(void);
void temp_limit(void);
u16 detCurrent(void);
void CurrLimit(void);
void timer2_init(u16,u16);
void detDDFOD(void);
void Caculatedata(void);
void SendOne(void);
void SendZero(void);
void send_one_byte(u8);
void Return_Ping(void);
void Ping(void);
void ID_Config(void);
void PID(u8);
void PWM_Handler(u8);
void Power_Transfer(void);
u16 Get_ADC_Average(u32);
u16 Get_ADC12bitResult(u32);
void Stop_Rec(void);
void delay_T0ms(u16 nns);
void anomaly_detect(void);
void send_nego_pack(u8 * arr,u8 length);
void negotiation_phase(void);
void clear_fod(void);
void mcu_init(void);
void app_mpp(void);
void GPIO_UART(u16);



#endif


